using System.Collections.Generic;
using UnityEngine;
using Voodoo.Pattern;

public class UpgradeBtns : MonoBehaviour
{
	public List<UpgradeBtn> btns;

	public int activeBtn;

	private bool delay;

	private float timer;

	public int ActiveBtn
	{
		get
		{
			return activeBtn;
		}
		set
		{
			if (delay)
			{
				delay = false;
				int activeBtnInd = value;
				if (value < 0)
				{
					activeBtnInd = btns.Count - 1;
				}
				else if (value > btns.Count - 1)
				{
					activeBtnInd = 0;
				}
				activeBtn = activeBtnInd;
				ShowActiveIcon(activeBtnInd);
			}
		}
	}

	private void Start()
	{
		ShowActiveIcon(0);
	}

	private void Update()
	{
		if (SingletonMB<GameManager>.Instance.AnyWindowActive)
		{
			return;
		}
		if (Singleton<InputController>.Instance.Action && btns[activeBtn].btn.interactable)
		{
			btns[activeBtn].btn.onClick.Invoke();
		}
		if (Singleton<InputController>.Instance.Horizontal == 0f)
		{
			delay = true;
			timer = 0f;
		}
		else if (Singleton<InputController>.Instance.Horizontal > 0f)
		{
			ActiveBtn++;
		}
		else if (Singleton<InputController>.Instance.Horizontal < 0f)
		{
			ActiveBtn--;
		}
		if (!delay)
		{
			timer += Time.deltaTime;
			if (timer > 0.3f)
			{
				delay = true;
				timer = 0f;
			}
		}
		if (Singleton<InputController>.Instance.Left)
		{
			ActiveBtn--;
			delay = true;
		}
		if (Singleton<InputController>.Instance.Right)
		{
			ActiveBtn++;
			delay = true;
		}
	}

	private void ShowActiveIcon(int activeBtnInd)
	{
		foreach (UpgradeBtn btn in btns)
		{
			btn.HideActiveIcon();
		}
		btns[activeBtnInd].ShowActiveIcon();
	}
}
